package kz.asoft;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.controller.TaskFlowId;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.component.rich.nav.RichCommandLink;
import oracle.adf.view.rich.render.ClientEvent;

public class TaskFlowManageBean {
    private double random;
    private String taskFlowId = "/WEB-INF/welcome-task-flow.xml#welcome-task-flow";

    public TaskFlowManageBean() {

    }

    public TaskFlowId getDynamicTaskFlowId() {
        return TaskFlowId.parse(taskFlowId);
    }

    public String risktaskflow() {
        taskFlowId = "/WEB-INF/risk-task-flow.xml#risk-task-flow";
        return null;
    }

    public String ctrlobjdatataskflow() {
        taskFlowId =
                "/WEB-INF/ctrl-obj-data-task-flow.xml#ctrl-obj-data-task-flow";
        return null;
    }

    public String welcometaskflow() {
        taskFlowId = "/WEB-INF/welcome-task-flow.xml#welcome-task-flow";
        return null;
    }

    public String quasistatetaskflow() {
        taskFlowId = "/WEB-INF/quasi-state-task-flow.xml#quasi-state-task-flow";
        return null;
    }

    public String coverctrlobjtaskflow() {
        taskFlowId = "/WEB-INF/cover-ctrlobj-task-flow.xml#cover-ctrlobj-task-flow";
        return null;
    }

    public String plntdctrltaskflow() {
        taskFlowId = "/WEB-INF/plntd-ctrl-task-flow.xml#plntd-ctrl-task-flow";
        return null;
    }

    public String plntdallctrltaskflow() {
        taskFlowId = "/WEB-INF/plntd-all-ctrl-task-flow.xml#plntd-all-ctrl-task-flow";
        return null;
    }

    public String riskreporttaskflow() {
        taskFlowId = "/WEB-INF/risk-report-task-flow.xml#risk-report-task-flow";
        return null;
    }

    public String refreshtaskflow() {
        if (taskFlowId.contains("cover-ctrlobj-task-flow.xml")){
            dbClickEvent("cl5");
        }else if (taskFlowId.contains("plntd-all-ctrl-task-flow.xml")){
            dbClickEvent("cl6");
        }
        setRandom(Math.random());
        return null;
    }

    public void setRandom(double random) {
        this.random = random;
    }

    public double getRandom() {
        return random;
    }
    
    /**
     * Navigate in left menu
     * @param componentIds
     */
    private void dbClickEvent(String componentIds) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot root = facesContext.getViewRoot();
        RichCommandLink link = (RichCommandLink)root.findComponent(componentIds);
        if (link != null) {
            ActionEvent actionEvent = new ActionEvent(link);
            actionEvent.queue();
        }
    }
}
